//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "FrameProvidersFinance.h"
#include "FormUtilities.h"
#include "FrameProviderRefundDlg.h"
#include "FrameProviderOrderDlg.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmFrameProvidersFinance::TfrmFrameProvidersFinance(TComponent* Owner, TFrameProviderBridge* bridge, TFrameProvider* frameProvider)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);
   this->bridge = bridge;
   this->frameProvider = frameProvider;
   this->Caption = "Frame providers finance - " + frameProvider->name;

   orders = new TStringList();
   orders->Duplicates = dupAccept;

   refunds = new TStringList();
   refunds->Duplicates = dupAccept;
}

void TfrmFrameProvidersFinance::SetOrderButtonsState()
{
   bool enabled = ( orders->Count > 0);

   btnUpdateOrder->Enabled = enabled;
   btnDeleteOrder->Enabled = enabled;
}

void TfrmFrameProvidersFinance::SetRefundButtonsState()
{
   bool enabled = ( refunds->Count > 0);
   
   btnUpdateRefund->Enabled = enabled;
   btnDeleteRefund->Enabled = enabled;
}

void TfrmFrameProvidersFinance::ShowTotalDebt()
{
   lblTotalDebtValue->Caption = GetTotalDebt();
   lblTotalDebtValue->Caption = lblTotalDebtValue->Caption + " " + CurrencyString;
}

//---------------------------------------------------------------------------
void __fastcall TfrmFrameProvidersFinance::btnUpdateOrderClick(TObject *Sender)
{
   TfrmFrameProviderOrderDlg * frmFrameProviderOrderDlg;
   TFrameOrder * order;

   order = ( TFrameOrder*)orders->Objects[ grdOrders->Row - 1];
   frmFrameProviderOrderDlg = new TfrmFrameProviderOrderDlg( this, bridge, order);

   try
   {
      if ( frmFrameProviderOrderDlg->ShowModal() == mrOk)
      {
         RefreshOrdersGrid();
         ShowTotalDebt();
      }
   }
   __finally
   {
      delete frmFrameProviderOrderDlg;
   }
}
//---------------------------------------------------------------------------
void __fastcall TfrmFrameProvidersFinance::btnAddOrderClick(TObject *Sender)
{
   TfrmFrameProviderOrderDlg * frmFrameProviderOrderDlg;

   frmFrameProviderOrderDlg = new TfrmFrameProviderOrderDlg( this, bridge, frameProvider->id);

   try
   {
      if ( frmFrameProviderOrderDlg->ShowModal() == mrOk)
      {
         RefreshOrdersGrid();
         ShowTotalDebt();
      }
   }
   __finally
   {
      delete frmFrameProviderOrderDlg;
   }
}

void TfrmFrameProvidersFinance::RefreshOrdersGrid()
{
   TFrameOrder* order;
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetOrders( frameProvider->id, orders);

   if ( orders->Count > 1)
      rowCount = orders->Count + 1;

   grdOrders->RowCount = rowCount;

   for ( i = 1; i < grdOrders->RowCount; i++)
      grdOrders->Rows[ i]->Clear();

   for ( i = 0; i < orders->Count; i++)
   {
      order = ( TFrameOrder*)orders->Objects[ i];

      grdOrders->Cells[ 0][ i + 1] = DateToStr( order->orderDate);
      AnsiString amount = order->amount;
      amount += " " + CurrencyString;
      grdOrders->Cells[ 1][ i + 1] = amount;
      grdOrders->Cells[ 2][ i + 1] = IntToStr( order->invoiceNumber);
   }//for

   TFormUtilities::SetLastColumnWidth( grdOrders);

   SetOrderButtonsState();
}

void TfrmFrameProvidersFinance::RefreshRefundsGrid()
{
   TFrameRefund* refund;
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetRefunds( frameProvider->id, refunds);

   if ( refunds->Count > 1)
      rowCount = refunds->Count + 1;

   grdRefunds->RowCount = rowCount;

   for ( i = 1; i < grdRefunds->RowCount; i++)
      grdRefunds->Rows[ i]->Clear();

   for ( i = 0; i < refunds->Count; i++)
   {
      refund = ( TFrameRefund*)refunds->Objects[ i];

      grdRefunds->Cells[ 0][ i + 1] = DateToStr( refund->refundDate);
      AnsiString amount = refund->amount;
      amount += " " + CurrencyString;
      grdRefunds->Cells[ 1][ i + 1] = amount;
   }//for

   TFormUtilities::SetLastColumnWidth( grdRefunds);
   
   SetRefundButtonsState();
}

void TfrmFrameProvidersFinance::RefreshGrids()
{
   RefreshOrdersGrid();
   RefreshRefundsGrid();
   ShowTotalDebt();
}

//---------------------------------------------------------------------------
void __fastcall TfrmFrameProvidersFinance::FormShow(TObject *Sender)
{
   grdOrders->Cells[ 0][ 0] = "DATE";
   grdOrders->Cells[ 1][ 0] = "AMOUNT";
   grdOrders->Cells[ 2][ 0] = "INVOICE NO.";

   grdRefunds->Cells[ 0][ 0] = "DATE";
   grdRefunds->Cells[ 1][ 0] = "AMOUNT";

   RefreshGrids();
}
//---------------------------------------------------------------------------
void __fastcall TfrmFrameProvidersFinance::btnDeleteOrderClick(TObject *Sender)
{
   TFrameOrder* order;

   order = ( TFrameOrder*)orders->Objects[ grdOrders->Row - 1];

   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
        bridge->DeleteOrder( order->id);
        delete order;
        orders->Delete( grdOrders->Row - 1);
        RefreshOrdersGrid();
        ShowTotalDebt();
   }
}

void __fastcall TfrmFrameProvidersFinance::btnCloseClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TfrmFrameProvidersFinance::FormDestroy(TObject *Sender)
{
   TStringUtilites::Clear( orders);
   delete orders;
   
   TStringUtilites::Clear( refunds);
   delete refunds;
}
//---------------------------------------------------------------------------

Currency TfrmFrameProvidersFinance::GetTotalDebt()
{
   int ordersCount = orders->Count;
   Currency ordersAmount = 0;

   for(int i = 0; i < ordersCount; i++)
   {
      TFrameOrder* order = (TFrameOrder*)orders->Objects[i];
      ordersAmount += order->amount;
   }

   int refundsCount = refunds->Count;
   Currency refundsAmount = 0;

   for(int i = 0; i < refundsCount; i++)
   {
      TFrameRefund* refund = (TFrameRefund*)refunds->Objects[i];
      refundsAmount += refund->amount;
   }

   return ordersAmount - refundsAmount;
}


void __fastcall TfrmFrameProvidersFinance::btnUpdateRefundClick(
      TObject *Sender)
{
   TfrmFrameProviderRefundDlg * frmFrameProviderRefundDlg;
   TFrameRefund * refund;

   refund = ( TFrameRefund*)refunds->Objects[ grdOrders->Row - 1];
   frmFrameProviderRefundDlg = new TfrmFrameProviderRefundDlg( this, bridge, refund);

   try
   {
      if ( frmFrameProviderRefundDlg->ShowModal() == mrOk)
      {
         RefreshRefundsGrid();
         ShowTotalDebt();
      }
   }
   __finally
   {
      delete frmFrameProviderRefundDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmFrameProvidersFinance::btnAddRefundClick(
      TObject *Sender)
{
   TfrmFrameProviderRefundDlg * frmFrameProviderRefundDlg;
   frmFrameProviderRefundDlg = new TfrmFrameProviderRefundDlg( this, bridge, frameProvider->id);

   try
   {
      if ( frmFrameProviderRefundDlg->ShowModal() == mrOk)
      {
         RefreshRefundsGrid();
         ShowTotalDebt();
      }
   }
   __finally
   {
      delete frmFrameProviderRefundDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmFrameProvidersFinance::btnDeleteRefundClick(
      TObject *Sender)
{
   TFrameRefund* refund;

   refund = ( TFrameRefund*)refunds->Objects[ grdRefunds->Row - 1];

   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
        bridge->DeleteRefund( refund->id);
        delete refund;
        refunds->Delete( grdRefunds->Row - 1);
        RefreshRefundsGrid();
        ShowTotalDebt();
   }
}
//---------------------------------------------------------------------------

